/*
 * $Id: $
 */
package org.rcgwt.client.ui.grid.renderer;

import org.rcgwt.client.ui.grid.internal.model.IHeaderModel;
import org.rcgwt.client.ui.grid.internal.model.ITitleRowModel;
import org.rcgwt.client.ui.grid.internal.model.IViewModel;
import org.rcgwt.client.ui.grid.internal.model.IViewModelElements;
import org.rcgwt.client.ui.grid.theme.CssStyle;
import org.rcgwt.client.ui.grid.theme.IGridTheme;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class DefaultHeaderRenderer extends AbstractRenderer<IHeaderModel>
        implements IHeaderRenderer {

    @Override
    public Element render(IViewRenderContext viewRenderContext,
            Element parentElement, IHeaderModel modelElement, int renderedIndex) {

        Document document = parentElement.getOwnerDocument();

        Element headerElement = document.createDivElement();

        update(viewRenderContext, headerElement, modelElement, -1);

        return headerElement;
    }

    @Override
    public void update(IViewRenderContext viewRenderContext,
            Element headerElement, IHeaderModel headerModel, int renderedIndex) {
        super.update(viewRenderContext, headerElement, headerModel,
                renderedIndex);

        viewRenderContext.getViewModel().getViewModelElements()
                .setHeaderElement(headerModel, headerElement);
    }

    @Override
    protected CssStyle getCssStyle(IViewRenderContext viewRenderContext,
            IHeaderModel headerModel) {
        return viewRenderContext.getGridRenderContext().getTheme()
                .getHeaderCssStyle(headerModel);

    }

    @Override
    protected void updateContent(IViewRenderContext viewRenderContext,
            Element headerElement, IHeaderModel headerModel) {

        IGridRenderContext gridRenderContext = viewRenderContext
                .getGridRenderContext();
        IGridTheme theme = gridRenderContext.getTheme();

        ITitleRowModel titleRowModels[] = headerModel.listTitleRowModels();

        assert titleRowModels != null : "TitleRowModels is NULL !";

        int renderedIndex = 0;
        for (ITitleRowModel titleRowModel : titleRowModels) {

            ITitleRowRenderer titleRowRenderer = theme
                    .getTitleRowRenderer(titleRowModel);

            Element titleElement = titleRowRenderer.render(viewRenderContext,
                    headerElement, titleRowModel, renderedIndex++);

            headerElement.appendChild(titleElement);
        }
    }

    @Override
    public void updateLayout(IViewRenderContext viewRenderContext,
            IHeaderModel headerModel, Element headerElement, int viewWidth,
            int viewHeight, int totalWidth, int[] columnWidths) {

        IGridRenderContext gridRenderContext = viewRenderContext
                .getGridRenderContext();

        IGridTheme gridTheme = gridRenderContext.getTheme();

        // IGrid grid = gridRenderContext.getGrid();

        Style headerElementStyle = headerElement.getStyle();
        int size = getSize(viewRenderContext, headerModel, headerElement);

        if (headerModel.getOrientation() == IHeaderModel.Orientation.HORIZONTAL) {
            headerElementStyle.setPropertyPx(Styles.WIDTH, viewWidth);

            if (size > 0) {
                headerElementStyle.setPropertyPx(Styles.HEIGHT, size);
            }
        } else {
            headerElementStyle.setPropertyPx(Styles.HEIGHT, viewHeight);

            if (size > 0) {
                headerElementStyle.setPropertyPx(Styles.WIDTH, size);
            }
        }

        IViewModel viewModel = viewRenderContext.getViewModel();
        IViewModelElements viewModelElements = viewModel.getViewModelElements();

        ITitleRowModel titleRowModels[] = headerModel.listTitleRowModels();

        for (ITitleRowModel titleRowModel : titleRowModels) {

            Element titleRowElement = viewModelElements
                    .getTitleRowElement(titleRowModel);

            ITitleRowRenderer titleRowRenderer = gridTheme
                    .getTitleRowRenderer(titleRowModel);

            titleRowRenderer.updateLayout(viewRenderContext, titleRowModel,
                    titleRowElement, totalWidth, columnWidths, size);
            
            // TODO Positionner les headers !!!!
        }
    }

    public int getSize(IViewRenderContext viewRenderContext,
            IHeaderModel headerModel, Element element) {

        IGridTheme gridTheme = viewRenderContext.getGridRenderContext().getTheme();

        IViewModel viewModel = viewRenderContext.getViewModel();
        IViewModelElements viewModelElements = viewModel.getViewModelElements();

        ITitleRowModel titleRowModels[] = headerModel.listTitleRowModels();

        int totalSize = 0;

        for (ITitleRowModel titleRowModel : titleRowModels) {

            Element titleRowElement = viewModelElements
                    .getTitleRowElement(titleRowModel);

            ITitleRowRenderer titleRowRenderer = gridTheme
                    .getTitleRowRenderer(titleRowModel);

            totalSize += titleRowRenderer.getSize(viewRenderContext,
                    titleRowModel, titleRowElement);
            
            // TODO Traiter le border ???
        }

        return totalSize;
    }
}
